using System;
using UnityEngine;
using UnityEngine.UI;

public class PlayerDie:MonoBehaviour
{
    [Header("死亡读秒")]
    public Text dieTimer;
    private void Update()
    {
        if(GameManager.Instance.playerDieData!=null&&GameManager.Instance.playerDieData.isDie)
        {
            int dieNum = Mathf.RoundToInt(GameManager.Instance.playerDieData.dieTimer);
            if (dieNum<0) dieNum=0;
            dieTimer.text=dieNum.ToString();
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.GetComponent<Player>()!=null)
        {
            if (GameManager.Instance.playerDieData!=null&&GameManager.Instance.playerDieData.isDie&&GameManager.Instance.playerDieData.dieTimer<=0)
            {
                //重置死亡数据
                GameManager.Instance.playerDieData.isDie=false;

                AddHalfPlayer();

                //销毁自己
                Destroy(gameObject);
            }
        }
    }
    //玩家属性恢复
    public void AddHalfPlayer()
    {
        Player player = GameManager.Instance.player;
        player.damage=player.dieDamage;
        player.oriDamage=player.dieOriDamage;
        player.armor=player.dieArmor;
        player.oriArmor=player.dieOriArmor;
        player.currHp=player.dieHp;
        player.oriHp=player.dieOriHp;

        if (player.currHp<=0) player.currHp=1;
        if (player.oriHp<=0) player.oriHp=1;

        //更新UI
        GameManager.Instance.UpdateMathData();

        //恢复人生体
        player.GetAniMaterial();
        //水域不可行走
        if (WaterMapManager.instance!=null)
        {
            WaterMapManager.instance.SetStop();
        }

        //提示
        TipSayManager.Instance.Say("找回了<color=yellow> 身躯 </color>");
        //音效，死而复生的
        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["复活"], 1f);
        //更新符文
        TurnManager.instance.UpdateModify();
    }
}

//死亡数据
[Serializable]
public class PlayerDieData
{
    //死亡倒计时
    public float dieTimer = 20;//20秒后才可以捡回墓碑
    //是否死亡
    public bool isDie = false;
    //死亡楼层和位置
    public int levelId;
    public Vector2 diePos;
}